package com.example.func.service.impl;

import com.example.func.dto.request.UserRequest;
import com.example.func.dto.response.UserResponse;
import com.example.func.service.UserService;
import com.example.func.vo.UserVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * 用户服务实现类
 * 实现用户相关的业务逻辑
 */
@Service
public class UserServiceImpl implements UserService {
    
    // 模拟数据库，使用内存存储
    private final List<UserResponse> userDatabase = new ArrayList<>();
    private final AtomicLong idGenerator = new AtomicLong(1);
    
    @Override
    public UserResponse createUser(UserRequest userRequest) {
        // 创建用户响应对象
        UserResponse userResponse = new UserResponse();
//        userResponse.setId(idGenerator.getAndIncrement());
        userResponse.setId(1L);
        userResponse.setName(userRequest.getName());
        userResponse.setAge(userRequest.getAge());
        userResponse.setEmail(userRequest.getEmail());
        userResponse.setPhone(userRequest.getPhone());
        userResponse.setCreateTime(getCurrentTime());
        userResponse.setUpdateTime(getCurrentTime());
        
        // 保存到模拟数据库
        userDatabase.add(userResponse);
        
        return userResponse;
    }
    
    @Override
    public UserResponse getUserById(Long id) {
        return userDatabase.stream()
                .filter(user -> user.getId().equals(id))
                .findFirst()
                .orElse(null);
    }
    
    @Override
    public List<UserVO> getUserList() {
        return userDatabase.stream()
                .map(this::convertToUserVO)
                .collect(Collectors.toList());
    }
    
    @Override
    public UserResponse updateUser(Long id, UserRequest userRequest) {
        UserResponse existingUser = getUserById(id);
        if (existingUser == null) {
            return null;
        }
        
        // 更新用户信息
        existingUser.setName(userRequest.getName());
        existingUser.setAge(userRequest.getAge());
        existingUser.setEmail(userRequest.getEmail());
        existingUser.setPhone(userRequest.getPhone());
        existingUser.setUpdateTime(getCurrentTime());
        
        return existingUser;
    }
    
    @Override
    public boolean deleteUser(Long id) {
        return userDatabase.removeIf(user -> user.getId().equals(id));
    }
    
    @Override
    public List<UserVO> getUserByName(String name) {
        return userDatabase.stream()
                .filter(user -> user.getName().contains(name))
                .map(this::convertToUserVO)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<UserVO> getUserByAgeRange(Integer minAge, Integer maxAge) {
        return userDatabase.stream()
                .filter(user -> user.getAge() >= minAge && user.getAge() <= maxAge)
                .map(this::convertToUserVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将UserResponse转换为UserVO
     * @param userResponse 用户响应对象
     * @return 用户VO对象
     */
    private UserVO convertToUserVO(UserResponse userResponse) {
        return new UserVO(
                userResponse.getId(),
                userResponse.getName(),
                userResponse.getAge(),
                userResponse.getEmail(),
                userResponse.getPhone()
        );
    }
    
    /**
     * 获取当前时间字符串
     * @return 当前时间字符串
     */
    private String getCurrentTime() {
        return java.time.LocalDateTime.now().format(
                java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        );
    }
}
